package cn.thrjit.service.impl;

import cn.dev33.satoken.secure.BCrypt;
import cn.thrjit.feign.AppLoginFeignClient;
import cn.thrjit.mapper.CustomerMapper;
import cn.thrjit.pojo.domain.*;
import cn.thrjit.pojo.param.RegisterSaveLoginParam;
import cn.thrjit.rocketmq.Constants;
import cn.thrjit.rocketmq.GlobalExceptionCode;
import cn.thrjit.service.ICustomerService;
import cn.thrjit.template.AppWeChatTemplate;
import cn.thrjit.utils.AssertUtil;
import cn.thrjit.utils.BitStatesUtil;
import cn.thrjit.utils.NameUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 客户 服务实现类
 * </p>
 *
 * @author xxx
 * @since 2024-07-31
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {
    @Autowired
    private AppWeChatTemplate appWeChatTemplate;
    @Autowired
    private CustomerSummaryServiceImpl customerSummaryService;
    @Autowired
    private CustomerWalletServiceImpl customerWalletService;
    @Autowired
    private AppLoginFeignClient appLoginFeignClient;
        @Override
        public void register(String openid, String phoneCode) {
            // 获取openId，校验用户是否已注册
            String openId = appWeChatTemplate.getOpenIdByCode(openid);

            Customer customer = super.getOne(new QueryWrapper<Customer>().eq("open_id", openid));

            AssertUtil.isNull(customer, GlobalExceptionCode.PARAM_PHONE_EXIST);
            // String phone =appWeChatTemplate.getPhoneByAccess(phoneCode); 个人开发者因前端无法获取code所有无法使用该方法
            customer = new Customer();
            // 保存这个customer对象和openid到数据库的方法
            saveCustomer(customer,openId,phoneCode);
            // 获取到保存的customer的id
            Long id = customer.getId();
            String password = "123456";
            // 使用satoken的BCrypt加密方式
            String hashpw = BCrypt.hashpw(password, BCrypt.gensalt());
            // 初始化从表把id传进去
            initDriverAssociation(id,hashpw);
            saveLogin(id,openId,phoneCode,hashpw,Constants.Login.TYPE_CUSTOMER);
        }


    private void saveLogin(Long id, String openId, String phoneCode, String password, int type) {
        RegisterSaveLoginParam registerSaveLoginParam = new RegisterSaveLoginParam();
        registerSaveLoginParam.setUsername(phoneCode);
        registerSaveLoginParam.setPassword(password);
        registerSaveLoginParam.setType(type);
        registerSaveLoginParam.setEnabled(false);
        registerSaveLoginParam.setAvatar(Constants.DEFAULT_AVATAR);
        registerSaveLoginParam.setAdmin(false);
        String name = NameUtil.getName();
        registerSaveLoginParam.setNickName(name);
        registerSaveLoginParam.setOpenId(openId);
        registerSaveLoginParam.setName(name);
        registerSaveLoginParam.setPhone(phoneCode);
        registerSaveLoginParam.setId(id);
        appLoginFeignClient.save(registerSaveLoginParam);
    }

    /**
     * 初始化从表
     * @param id
     */
    private void initDriverAssociation(Long id,String hashpw) {
        // 清空从表
        CustomerWallet customerWallet = new CustomerWallet();
        customerWallet.setId(id);
        customerWallet.setAmount(new BigDecimal("0"));
        customerWallet.setPassword(hashpw);
        customerWallet.setDeleted(false);
        customerWallet.setVersion(0);
        customerWallet.setCreateTime(new Date());
        customerWalletService.save(customerWallet);

        // 清空从表
        CustomerSummary customerSummary = new CustomerSummary();
        customerSummary.setId(0L);
        customerSummary.setTotalOrders(0);
        customerSummary.setTotalFine(new BigDecimal("0"));
        customerSummary.setVoucherNumbers(0);
        customerSummary.setCancelOrders(0);
        // 保存从表对象
        customerSummaryService.save(customerSummary);


    }

    /**
         * 保存customer乘客对象
         * @param customer
         */
        private void saveCustomer(Customer customer,String openId, String phone) {
            // 设置默认值
            customer.setSex(Constants.SEX_MAN);
            customer.setPhone(phone);
            customer.setCreateTime(new Date());
            customer.setVersion(0);
            customer.setDeleted(false);
            customer.setOpenId(openId);
            customer.setBitState(BitStatesUtil.OP_PHONE);
            customer.setName(NameUtil.getName());
            customer.setLevel(Constants.Level.LEVEL_BRONZE);
            // 通过父类方法保存对象
            super.save(customer);
        }
}
